/******************************************************************************
 * Copyright (C) 2016 Wuhan Water Elephant Co.Ltd All Rights Reserved. 
 * 本软件为武汉水象科技有限公司开发研制。 未经本公司正式书面同意，其他任何个人、
 * 团体不得使用、复制、修改或发布本软件.
 *****************************************************************************/
package com.sxfq.loansupermarket.app.personal.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sxfq.loansupermarket.app.personal.service.PersonalService;
import com.sxfq.loansupermarket.app.platform.service.PlatformService;
import com.sxfq.loansupermarket.business.base.SqlMapper;
import com.sxfq.loansupermarket.business.customer.entity.CustomerBaseInfo;
import com.sxfq.loansupermarket.business.customer.entity.CustomerBorrowInfo;
import com.sxfq.loansupermarket.business.customer.entity.CustomerFeedback;
import com.sxfq.loansupermarket.business.customer.entity.CustomerPlatformVisitInfo;
import com.sxfq.loansupermarket.business.customer.service.CustomerBaseInfoService;
import com.sxfq.loansupermarket.business.customer.service.CustomerBorrowInfoService;
import com.sxfq.loansupermarket.business.customer.service.CustomerFeedbackService;
import com.sxfq.loansupermarket.business.customer.service.CustomerPlatformVisitInfoService;
import com.sxfq.loansupermarket.business.platform.entity.PlatformProductInfo;
import com.sxfq.loansupermarket.business.platform.service.PlatformProductInfoService;
import com.sxfq.loansupermarket.common.utils.DateUtil;
import com.sxfq.loansupermarket.common.utils.DoubleUtil;
import com.sxfq.loansupermarket.common.utils.StringUtil;

/**
 * 
 * 
 * Module:
 * 
 * PersonalServiceImpl.java
 * 
 * @author 程盼
 * @since JDK 1.8
 * @version 1.0
 * @description: <描述>
 */
@Service
public class PersonalServiceImpl implements PersonalService {

	@Resource
	private SqlMapper sqlMapper;

	@Resource
	private CustomerBaseInfoService customerBaseInfoService;

	@Resource
	private CustomerBorrowInfoService customerBorrowInfoService;

	@Resource
	private CustomerFeedbackService customerFeedbackService;

	@Resource
	private CustomerPlatformVisitInfoService customerPlatformVisitInfoService;

	@Resource
	private PlatformProductInfoService platformProductInfoService;

	@Resource
	private PlatformService platFormService;

	/**
	 * 查询用户借款信息
	 * 
	 * @param borrowerId
	 */
	@Override
	public Map<String, Object> getBorrowInfo(String borrowerId) {
		CustomerBorrowInfo entity = new CustomerBorrowInfo();
		entity.setBorrowerId(Long.parseLong(borrowerId));
		CustomerBorrowInfo record = customerBorrowInfoService.selectOne(entity);
		Map<String, Object> map = new HashMap<String, Object>();
		if (null != record) {
			map.put("borrowerId", borrowerId);// 借款人ID
			map.put("expectedAmount", DoubleUtil.toZeroDecimal(record.getExpectedAmount()));// 期望金额
			map.put("borrowCycle", StringUtil.toString(record.getBorrowCycle()));// 借款周期
			map.put("paymentDate", StringUtil.toString(record.getPaymentDate()));// 到账时间
		} else {
			map.put("borrowerId", borrowerId);// 借款人ID
			map.put("expectedAmount", "");// 期望金额
			map.put("borrowCycle", "");// 借款周期
			map.put("paymentDate", "");// 到账时间
		}

		return map;
	}

	/**
	 * 保存用户借款信息
	 * 
	 * @see com.sxfq.loansupermarket.app.personal.service.PersonalService#saveBorrowInfo(com.sxfq.loansupermarket.business.customer.entity.CustomerBorrowInfo)
	 */
	@Override
	public void saveBorrowInfo(CustomerBorrowInfo entity) {
		CustomerBorrowInfo queryEntity = new CustomerBorrowInfo();
		queryEntity.setBorrowerId(entity.getBorrowerId());
		CustomerBorrowInfo record = customerBorrowInfoService.selectOne(queryEntity);
		if (null != record) {
			entity.setId(record.getId());
			entity.setUpdateTime(new Date());
			customerBorrowInfoService.updateByPrimaryKeySelective(entity);
		} else {
			CustomerBaseInfo customerBaseInfo = sqlMapper.selectOne(
					"select * from customer_base_info where borrower_id=" + entity.getBorrowerId(),
					CustomerBaseInfo.class);
			if (null != customerBaseInfo) {
				entity.setCustomerNo(customerBaseInfo.getCustomerNo());// 客户编号
				entity.setPhone(customerBaseInfo.getPhone());// 手机号
				entity.setName(customerBaseInfo.getName());// 姓名
			}
			entity.setCreateTime(new Date());
			entity.setUpdateTime(new Date());
			customerBorrowInfoService.insert(entity);
		}
	}

	/**
	 * 保存用户访问记录
	 * 
	 * @see com.sxfq.loansupermarket.app.personal.service.PersonalService#saveCustomerPlatformVisitInfo(com.sxfq.loansupermarket.business.customer.entity.CustomerPlatformVisitInfo)
	 */
	@Override
	public void saveCustomerPlatformVisitInfo(CustomerPlatformVisitInfo entity) {
		if (!StringUtil.isEmpty(entity.getPlatformId())) {
			Long platformId = entity.getPlatformId();
			PlatformProductInfo platform = platformProductInfoService.selectByPrimaryKey(platformId);
			entity.setCompanyId(platform.getCompanyId());
		}
		entity.setAccessTime(new Date());
		entity.setCreateTime(new Date());
		entity.setUpdateTime(new Date());
		customerPlatformVisitInfoService.insert(entity);
	}

	/**
	 * 获取用户浏览产品记录
	 * 
	 * @see com.sxfq.loansupermarket.app.personal.service.PersonalService#getAccessRecord()
	 */
	@Override
	public List<Map<String, Object>> getCustomerPlatformVisitInfo(String borrwerId, int pageNum, int pageSize) {
		String sql = "select c.abbreviation,c.logo,a.access_time accessTime,b.min_quato minQuato,b.max_quato maxQuato,a.platform_id platFormId from customer_platform_visit_info a left join platform_product_info b on a.platform_id=b.id left join platform_company_info c on b.company_id=c.id  where a.platform_id is not null and borrower_id="
				+ borrwerId + " order by accessTime desc limit " + (pageNum - 1) * pageSize + "," + pageSize;
		List<Map<String, Object>> platFormList = sqlMapper.selectList(sql);
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		for (Map<String, Object> map : platFormList) {
			Map<String, Object> platFormMap = new HashMap<String, Object>();
			platFormMap.put("platformId", StringUtil.toString(map.get("platFormId")));// 平台ID
			platFormMap.put("accessTime",
					DateUtil.getDateString(StringUtil.toString(map.get("accessTime")), DateUtil.yyyy_MM_dd));
			platFormMap.put("abbreviation", StringUtil.toString(map.get("abbreviation")));// 公司名称
			platFormMap.put("logo", StringUtil.toString(map.get("logo")));// logo
			platFormMap.put("quato",
					getQuato(StringUtil.toString(map.get("minQuato")), StringUtil.toString(map.get("maxQuato"))));// 放款额度
			// 查询标签
			String label = platFormService.getPlatformLabel(StringUtil.toString(map.get("platFormId")));
			platFormMap.put("label", label);// 标签
			list.add(platFormMap);
		}
		return list;
	}

	/**
	 * 查询用户基本资料
	 */
	@Override
	public Map<String, Object> getCustomerBaseInfo(String borrwerId) {
		CustomerBaseInfo queryEntity = new CustomerBaseInfo();
		queryEntity.setBorrowerId(Long.parseLong(borrwerId));
		CustomerBaseInfo record = customerBaseInfoService.selectOne(queryEntity);
		if (null == record) {
			record = new CustomerBaseInfo();
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", StringUtil.toString(record.getName()));// 姓名
		if (!StringUtil.isEmpty(record.getIdCard())) {
			String idCard = StringUtil.toString(record.getIdCard());
			idCard = idCard.substring(0, 3) + "***********" + idCard.substring(idCard.length() - 4, idCard.length());
			map.put("idCard", idCard);// 身份证号
		} else {
			map.put("idCard", StringUtil.toString(record.getIdCard()));// 身份证号
		}

		return map;
	}

	/**
	 * 
	 * @see com.sxfq.loansupermarket.app.personal.service.PersonalService#saveCustomerBaseInfo(com.sxfq.loansupermarket.business.customer.entity.CustomerBaseInfo)
	 */
	@Override
	public void saveCustomerBaseInfo(CustomerBaseInfo entity) {
		CustomerBaseInfo queryEntity = new CustomerBaseInfo();
		queryEntity.setBorrowerId(entity.getBorrowerId());
		CustomerBaseInfo record = customerBaseInfoService.selectOne(queryEntity);
		if (null != record) {
			entity.setId(record.getId());
			entity.setUpdateTime(new Date());
			customerBaseInfoService.updateByPrimaryKeySelective(entity);
		} else {
			Integer maxNo = sqlMapper.selectOne("select max(customer_no) from customer_base_info", Integer.class);
			if (StringUtil.toInteger(maxNo) == 0) {
				maxNo = 10000;
			}
			entity.setCustomerNo(StringUtil.toString(StringUtil.toInteger(maxNo) + 1));
			entity.setCreateTime(new Date());
			entity.setUpdateTime(new Date());
			customerBaseInfoService.insert(entity);
		}
	}

	/**
	 * 
	 * @see com.sxfq.loansupermarket.app.personal.service.PersonalService#saveCustomerFeedBack(com.sxfq.loansupermarket.business.customer.entity.CustomerFeedback)
	 */
	@Override
	public void saveCustomerFeedBack(CustomerFeedback entity) {
		CustomerFeedback queryEntity = new CustomerFeedback();
		queryEntity.setBorrowerId(entity.getBorrowerId());
		CustomerFeedback record = customerFeedbackService.selectOne(queryEntity);
		if (null != record) {
			entity.setId(record.getId());
			entity.setUpdateTime(new Date());
			customerFeedbackService.updateByPrimaryKeySelective(entity);
		} else {
			entity.setCreateTime(new Date());
			entity.setUpdateTime(new Date());
			customerFeedbackService.insert(entity);
		}
	}

	private String getQuato(String minQuato, String maxQuato) {
		String quato = "";
		if (!StringUtil.isEmpty(minQuato)) {
			quato = DoubleUtil.toZeroDecimal(Double.valueOf(minQuato));
		}
		if (!StringUtil.isEmpty(maxQuato)&&!maxQuato.equals("0.00")) {
			quato = DoubleUtil.toZeroDecimal(Double.valueOf(maxQuato));
		}
		if (!StringUtil.isEmpty(minQuato) && !StringUtil.isEmpty(maxQuato)&&!maxQuato.equals("0.00")) {
			quato = DoubleUtil.toZeroDecimal(Double.valueOf(minQuato)) + "~"
					+ DoubleUtil.toZeroDecimal(Double.valueOf(maxQuato));
		}
		return quato;
	}

}
